package com.mycompony.shopping.shangguan.sys.action;

import org.apache.commons.lang.StringUtils;

import com.mycompony.shopping.shangguan.sys.context.SessionUtil;
import com.mycompony.shopping.shangguan.sys.model.Password;
import com.mycompony.shopping.shangguan.sys.model.User;
import com.mycompony.shopping.shangguan.sys.service.UserService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class UserAction extends ActionSupport {

	private User user;
	private Password password;
	private String userVerificationCode;

	public String login() throws Exception {

		String authCode = (String) ActionContext.getContext().getSession().get(AuthCodeAction.AuthCodeKEY);

		if (StringUtils.isEmpty(userVerificationCode)) {
			return ActionSupport.INPUT;
		}

		if (StringUtils.isEmpty(authCode)) {
			return ActionSupport.INPUT;
		}

		if (!authCode.equals(userVerificationCode)) {
			return ActionSupport.INPUT;
		}

		User usetTemp = userService.loginUser(user, password);
		if (usetTemp == null) {
			return ActionSupport.INPUT;
		}
		setUser(usetTemp);
		SessionUtil.putCurrentUser(usetTemp);
	
		return ActionSupport.SUCCESS;
	}

	private static final long serialVersionUID = -4519729763570830416L;
	private UserService userService;

	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public Password getPassword() {
		return password;
	}

	public void setPassword(Password password) {
		this.password = password;
	}

	public String getUserVerificationCode() {
		return userVerificationCode;
	}

	public void setUserVerificationCode(String userVerificationCode) {
		this.userVerificationCode = userVerificationCode;
	}

}
